/*
 *  charprinter.cpp
 */

#include <iostream>
#include "charprinter.h"

CharPrinter::CharPrinter(int id, char* buf, int* current_index, Mutex* mutex)
{
    this->id = id;
    this->buf = buf;
    this->current_index = current_index;
    this->mutex = mutex;
}

CharPrinter::~CharPrinter() {}

void CharPrinter::run() {
    char keepPrinting = 1;
    while (keepPrinting) {
        mutex->lock();
        char c = buf[*current_index];
        if (c) {
            std::cout << "[thread " << id << "] Letter " << c << std::endl;
            ++(*current_index);
        } else {
            std::cout << "[thread " << id << "] buff end detected" << std::endl;
            keepPrinting = 0;
        }
        mutex->unlock();
    }
}

//{+}

